<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="注册日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="服务商名称" prop="providersName" label-width="100px">
        <el-input
          v-model="queryParams.providersName"
          placeholder="服务商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      两下拉查询只需要传给后端id&code就行了-->
      <!--      这个下拉改成字典服务状态下拉-->
      <el-form-item label="服务状态" prop="serviceStatus">
        <el-select v-model="queryParams.serviceStatus" placeholder="请选择" filterable popper-class="my-dropdown" clearable
                   :value-key="'code'">
          <el-option v-for="item in serviceStatusListSelectOptions" :key="item.code" :label="item.name"
                     :value="item.code"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStatus(scope.row,'3')"
          >禁用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStatus(scope.row,'1')"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'edit')"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'detail')"
          >详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="id" prop="registerId" align="center" width="350"/>
      <el-table-column label="邮箱" prop="email" align="center" width="150"/>
      <el-table-column label="注册电话" prop="phoneAreaInfo" align="center" width="150"/>
      <el-table-column label="昵称" prop="nickName" align="center" width="150"/>
      <el-table-column label="推荐人" prop="referenceName" align="center" width="150"/>
      <el-table-column label="注册渠道" prop="registerChannelName" align="center" width="150"/>
      <el-table-column label="状态" prop="statusName" align="center" width="150"/>
      <el-table-column label="实名时间" prop="realNameTime" align="center" width="150"/>
      <el-table-column label="注册时间" prop="createTime" align="center" width="150"/>
      <el-table-column label="描述" prop="description" align="center" width="350" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--     添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1020px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <span style="font-weight: bold; color: #333;position: relative;top: -15px;font-size: 15px;left: 41px;">基本信息
          <span
            style="position: absolute;display: inline-block; width: 1321%; border-bottom: 1px solid #ccc;margin-top: 9px;"></span></span>
        <!--       新增编辑部分-->
        <el-row v-if="showAddOrUpdateView">
          <el-row>
            <el-col :span="20">
              <el-form-item label="id" prop="registerId">
                <el-input v-model="form.registerId" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="注册邮箱" prop="email">
                <el-input v-model="form.email"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="注册电话" prop="phoneArea">
                <el-select v-model="phoneArea" placeholder="请选择" filterable popper-class="my-dropdown"
                           :value-key="'code'">
                  <el-option v-for="item in phoneAreaSelectOptions" :key="item.code" :label="item.name"
                             :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10" style="margin-left: -20%">
              <el-form-item label="" prop="phone">
                <el-input v-model="form.phone"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="昵称" prop="nickName">
                <el-input v-model="form.nickName"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="注册渠道" prop="registerChannel">
                <el-select v-model="registerChannel" placeholder="请选择" filterable popper-class="my-dropdown"
                           :value-key="'code'">
                  <el-option v-for="item in registerChannelSelectOptions" :key="item.code" :label="item.name"
                             :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="推荐人" prop="referenceName">
                <el-input v-model="form.referenceName"/>
              </el-form-item>
            </el-col>
            <el-col :span="10" v-if="showCreateByName">
              <el-form-item label="注册时间" prop="createTime">
                <el-input v-model="form.createTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <span v-if="showModifyMsg"
                style="font-weight: bold; color: #333;position: relative;top: -15px;font-size: 15px;left: 41px;">更新信息
          <span
            style="position: absolute;display: inline-block; width: 1321%; border-bottom: 1px solid #ccc;margin-top: 9px;"></span></span>


          <el-row>
            <el-col :span="20">
              <el-form-item label="备注" prop="description">
                <el-input v-model="form.description"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row v-if="showModifyMsg">
            <el-col :span="10">
              <el-form-item label="注册人" prop="createByName">
                <el-input v-model="formView.createByName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="注册时间" prop="createTime">
                <el-input v-model="formView.createTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

        </el-row>
        <!--        详情部分-->
        <el-row v-if="showDetailView">
          <el-row>
            <el-col :span="10">
              <el-form-item label="id" prop="registerId">
                <el-input v-model="formView.registerId" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="状态" prop="statusName">
                <el-input v-model="formView.statusName" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="注册邮箱" prop="email">
                <el-input v-model="formView.email" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="注册电话" prop="phoneInfo">
                <el-input v-model="formView.phoneInfo" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="昵称" prop="nickName">
                <el-input v-model="formView.nickName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="注册渠道" prop="registerChannelName">
                <el-input v-model="formView.registerChannelName" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="推荐人" prop="referenceName">
                <el-input v-model="formView.referenceName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="实名时间" prop="realNameTime">
                <el-input v-model="formView.realNameTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>
          <span style="font-weight: bold; color: #333;position: relative;top: -15px;font-size: 15px;left: 41px;">禁用启用
          <span
            style="position: absolute;display: inline-block; width: 1321%; border-bottom: 1px solid #ccc;margin-top: 9px;"></span></span>

          <el-row>
            <el-col :span="10">
              <el-form-item label="禁用人" prop="disabledByName">
                <el-input v-model="formView.disabledByName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="禁用时间" prop="disabledTime">
                <el-input v-model="formView.disabledTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="启用人" prop="enableByName">
                <el-input v-model="formView.enableByName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="启用时间" prop="enableTime">
                <el-input v-model="formView.enableTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <span style="font-weight: bold; color: #333;position: relative;top: -15px;font-size: 15px;left: 41px;">更新信息
          <span
            style="position: absolute;display: inline-block; width: 1321%; border-bottom: 1px solid #ccc;margin-top: 9px;"></span></span>

          <el-row>
            <el-col :span="20">
              <el-form-item label="备注" prop="description">
                <el-input v-model="formView.description" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="修改人" prop="modifyByName">
                <el-input v-model="formView.modifyByName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="修改时间" prop="modifyTime">
                <el-input v-model="formView.modifyTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="10">
              <el-form-item label="注册人" prop="createByName">
                <el-input v-model="formView.createByName" :disabled="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="注册时间" prop="createTime">
                <el-input v-model="formView.createTime" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>

        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="showBtn">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCustomer, getCustomer, addCustomer, updateCustomer, editStatus} from "@/api/system/registerCustomer";
import {getUnique} from "@/api/system/uniqueValue";
import {getNationalSelect, getServiceStatusSelect} from "@/api/system/selectList";
import {addDateRange2} from "@/utils/ruoyi";
import {MessageBox} from 'element-ui';

export default {
  name: "National",
  data() {
    return {
      //下拉选项
      //模态框归属国家的下拉
      modifyMessage: '',
      //列表搜索框服务状态下拉
      serviceStatusListSelectOptions: [],
      //列表搜索框国家的下拉
      nationalIdListSelectOptions: [],
      //模态框注册电话国际号码下拉
      phoneArea: [],
      phoneAreaSelectOptions: [],
      //注册渠道下拉
      registerChannel: [],
      registerChannelSelectOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      //显示条件
      showBtn: true,
      showModifyTime: true,
      showModifyByName: true,
      showModifyDescription: true,
      showModifyMsg: true,
      showCreateTime: true,
      showCreateByName: true,
      // 非多个禁用
      multiple: true,
      showAddOrUpdateView: true,
      showDetailView: true,
      // 显示搜索条件
      showSearch: true,
      dateRange: [],
      dateRange2: [],
      // 总条数
      total: 0,
      // 公告表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        nickname: undefined,
        channel: undefined
      },
      // 表单参数
      form: {},
      formView: {},
      // 表单校验
      rules: {
        providersName: [
          {required: true, message: "服务商名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleStatus(row, status) {
      const id = row.id || this.ids;
      const statusStr = status == '3' ? '禁用' : '启用';
      var email = row.email;
      // 使用 Element UI 的 MessageBox 弹出确认框
      MessageBox.confirm(`是否"${statusStr}"邮箱为"${email}"的注册客户，请确认此次的操作`, '警告信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 用户点击确定按钮时执行的操作
        editStatus(id, status).then(response => {
          this.$modal.msgSuccess(statusStr + "成功");
          this.getList();
        });
      }).catch(() => {
        // 用户点击取消按钮时执行的操作
      });
    },
    getImgOpenPage(obj) {
      var url = obj;
      window.open(url);
    },
    getStatusColor(state) {
      // 根据状态值动态返回颜色
      return state == 0 ? 'red' : 'green';
    },
    getList() {
      this.loading = true;
      listCustomer(addDateRange2(this.queryParams, this.dateRange, this.dateRange2)).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getServiceStatusSelect('REGISTER_CUSTOMERS_STATUS').then(response => {
        this.serviceStatusListSelectOptions = response.data;
      });
      getServiceStatusSelect('REGISTER_PHONE_AREA').then(response => {
        this.phoneAreaSelectOptions = response.data;
      });
      getServiceStatusSelect('REGISTER_CHANNEL').then(response => {
        this.registerChannelSelectOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //清空日历控件
      this.dateRange = [];
      this.dateRange2 = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-based
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.modifyMessage = "其他";
      this.showAddOrUpdateView = true;
      this.showDetailView = false;
      getUnique().then(response => {
        this.form.registerId = response;
        this.open = true;
        this.title = "添加";
        // this.nationalId = null;
        this.form.createTime = this.formatDate(new Date());
      });
      //添加则不显示创建人创建时间跟修改信息
      this.showBtn = true;
      this.showModifyTime = false;
      this.showModifyByName = false;
      this.showModifyDescription = false;
      this.showModifyMsg = false;
      this.showCreateTime = false;
      this.showCreateByName = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, status) {
      this.modifyMessage = "变更";
      this.showModifyMsg = true;
      this.showCreateTime = true;
      this.showCreateByName = false;
      this.showModifyDescription = true;

      if (status == 'edit') {
        //修改人和修改时间隐藏
        this.showBtn = true;
        this.showModifyTime = false;
        this.showModifyByName = false;
        this.showAddOrUpdateView = true;
        this.showDetailView = false;
      } else if (status == 'detail') {
        //全显示且保存隐藏
        this.showAddOrUpdateView = false;
        this.showDetailView = true;
        this.showBtn = false;
        this.showModifyTime = true;
        this.showModifyByName = true;
      }
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.formView = response.data;
        this.open = true;
        this.title = "修改";
        //下拉回显
        // this.nationalId = response.data.nationalName;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.phoneArea != null && this.phoneArea.code != null) {
            this.form.phoneArea = this.phoneArea.code;
          }
          if (this.registerChannel != null && this.registerChannel.code != null) {
            this.form.registerChannel = this.registerChannel.code;
          }
          if (this.form.id != undefined) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function () {
        return delProviders(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
<style>
.my-dropdown .el-popper {
  /* 控制下拉框的弹出方向 */
  /* 例如，下拉框向上弹出 */
  top: -50px !important;
  bottom: 100% !important;
}

.image-container {
  text-align: left;
}

.image-item {
  display: inline-block; /* 设置为行内块元素 */
  width: 33.33%; /* 每行最多显示三张图片 */
  margin-bottom: 10px; /* 图片之间的间距 */
}

</style>
